using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Transmute.Graphics;

namespace DemoApp
{
    public class Game1 : Game
    {
        #region Properties

        GraphicsDeviceManager graphics;
        SpriteFont font;

        Texture2D line;
        Texture2D rectangle;
        Texture2D filledRectangle;
        Texture2D circle;
        Texture2D filledCircle;
        Texture2D ellipse;
        Texture2D filledEllipse;
        Texture2D triangle;        
        Texture2D gradient;
        Texture2D radialGradient;
        Texture2D diamondGradient;
        Texture2D angleGradient;

        #endregion

        #region Constructor

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this)
            {
                PreferredBackBufferWidth = 1280,
                PreferredBackBufferHeight = 720
            };
            Content.RootDirectory = "Content";
            Window.Title = "Transmute.Graphics Demo";            
        }

        #endregion

        #region Methods

        protected override void Initialize()
        {
            GraphicsHelper.Initialize(GraphicsDevice);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            font = Content.Load<SpriteFont>("DefaultFont");

            line = GraphicsHelper.RenderToTexture(128, 128, () => { DrawLine(); });
            rectangle = GraphicsHelper.RenderToTexture(128, 128, () => { DrawRectangle(); });
            filledRectangle = GraphicsHelper.RenderToTexture(128, 128, () => { DrawFilledRectangle(); });
            circle = GraphicsHelper.RenderToTexture(128, 128, () => { DrawCircle(); });
            filledCircle = GraphicsHelper.RenderToTexture(128, 128, () => { DrawFilledCircle(); });
            ellipse = GraphicsHelper.RenderToTexture(128, 128, () => { DrawEllipse(); });
            filledEllipse = GraphicsHelper.RenderToTexture(128, 128, () => { DrawFilledEllipse(); });
            triangle = GraphicsHelper.RenderToTexture(128, 128, () => { DrawTriangle(); });
            gradient = new LinearGradientBrush(128, 128, Color.Red, Color.Purple);
            radialGradient = new RadialGradientBrush(128, 128, new Vector2(64, 64), Color.Blue, Color.Green);
            diamondGradient = new DiamondGradientBrush(128, 128, new Vector2(64, 64), Color.Yellow, Color.Red);
            angleGradient = new AngleGradientBrush(128, 128, new Vector2(64, 64), Color.Black, Color.White);
        }
                
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            var spriteBatch = GraphicsHelper.SpriteBatch;

            spriteBatch.Begin();
                
                // Line
                spriteBatch.Draw(line, new Vector2(50, 50), Color.White);
                spriteBatch.DrawString(font, "Line", new Vector2(50, 178), Color.White);  
  
                // Rectangle
                spriteBatch.Draw(rectangle, new Vector2(194, 50), Color.White);
                spriteBatch.DrawString(font, "Rectangle", new Vector2(194, 178), Color.White);  

                // Filled Rectangle
                spriteBatch.Draw(filledRectangle, new Vector2(338, 50), Color.White);
                spriteBatch.DrawString(font, "Filled\r\nRectangle", new Vector2(338, 178), Color.White);  

                // Circle
                spriteBatch.Draw(circle, new Vector2(482, 50), Color.White);
                spriteBatch.DrawString(font, "Circle", new Vector2(482, 178), Color.White);  

                // Filled Circle
                spriteBatch.Draw(filledCircle, new Vector2(626, 50), Color.White);
                spriteBatch.DrawString(font, "Filled\r\nCircle", new Vector2(626, 178), Color.White);  

                // Ellipse
                spriteBatch.Draw(ellipse, new Vector2(770, 50), Color.White);
                spriteBatch.DrawString(font, "Ellipse", new Vector2(770, 178), Color.White);  

                // Filled Ellipse
                spriteBatch.Draw(filledEllipse, new Vector2(914, 50), Color.White);
                spriteBatch.DrawString(font, "Filled\r\nEllipse", new Vector2(914, 178), Color.White);  

                // Triangle
                spriteBatch.Draw(triangle, new Vector2(1058, 50), Color.White);
                spriteBatch.DrawString(font, "Triangle", new Vector2(1058, 178), Color.White);  

                // Gradient
                spriteBatch.Draw(gradient, new Vector2(50, 242), Color.White);
                spriteBatch.DrawString(font, "Linear\r\nGradient", new Vector2(50, 370), Color.White);

                // Radial Gradient
                spriteBatch.Draw(radialGradient, new Vector2(194, 242), Color.White);
                spriteBatch.DrawString(font, "Radial\r\nGradient", new Vector2(194, 370), Color.White);

                // Diamond Gradient
                spriteBatch.Draw(diamondGradient, new Vector2(338, 242), Color.White);
                spriteBatch.DrawString(font, "Diamond\r\nGradient", new Vector2(338, 370), Color.White);

                // Angle Gradient
                spriteBatch.Draw(angleGradient, new Vector2(482, 242), Color.White);
                spriteBatch.DrawString(font, "Angle\r\nGradient", new Vector2(482, 370), Color.White);  

            spriteBatch.End();

            base.Draw(gameTime);
        }

        #endregion

        #region GraphicsHelper RenderToTexture Methods

        public void DrawLine()
        {
            GraphicsHelper.DrawLine(4, 4, 124, 124, 4, Color.Red);
        }

        public void DrawRectangle()
        {
            GraphicsHelper.DrawRectangle(4, 4, 120, 120, 4, Color.Green);
        }

        public void DrawFilledRectangle()
        {
            GraphicsHelper.DrawFilledRectangle(4, 4, 120, 120, 4, Color.Red, Color.Green);
        }

        public void DrawCircle()
        {
            GraphicsHelper.DrawCircle(50, 50, 50, 3, Color.Blue);
        }

        public void DrawFilledCircle()
        {
            GraphicsHelper.DrawFilledCircle(52, 52, 50, 1, Color.Green, Color.Blue);
        }

        public void DrawEllipse()
        {
            GraphicsHelper.DrawEllipse(52, 52, 50, 25, 3, Color.Orange);
        }

        public void DrawFilledEllipse()
        {
            GraphicsHelper.DrawFilledEllipse(52, 52, 50, 25, 1, Color.Indigo, Color.Orange);
        }

        public void DrawTriangle()
        {
            GraphicsHelper.DrawTriangle(new Vector2(0, 64), new Vector2(0, 128), new Vector2(128, 128), 1, Color.Yellow);
        }

        #endregion
    }
}
